package com.awspaas.user.apps.cpq.event;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Row;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.core.delegate.ProcessExecutionContext;
import com.actionsoft.bpms.bpmn.engine.listener.ExecuteListener;
import com.actionsoft.bpms.bpmn.engine.listener.ListenerConst;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;

public class ComponentFromAfterSave extends ExecuteListener{

	public ComponentFromAfterSave() {
		setDescription("将物料单价台账中，状态为拟制的数据更新为启用");
	}

	@Override
	public void execute(ProcessExecutionContext pec) throws Exception {
		String bindId=pec.getProcessInstance().getId();
		//BO表名
        String boName = pec.getParameterOfString(ListenerConst.FORM_EVENT_PARAM_BONAME);
        if(boName.equals("BO_EU_CPQ_COMPONENT_PART")) {
        	BO data=SDK.getBOAPI().query("BO_EU_CPQ_COMPONENT_PART").bindId(bindId).detail();
        	String status=data.getString("STATUS");
        	if(status!=null&&status.equals("拟制")) {
        		data.set("STATUS", "启用");
        		SDK.getBOAPI().update("BO_EU_CPQ_COMPONENT_PART", data);
        	}
        	
        	String type=data.getString("TYPE");
        	List<BO> lists=new ArrayList<BO>();
        	if(type.equals("制造件")) {
        		//获取材料费的数据并件数据回写到材料用量信息子表中。
        		List<BO> bos=SDK.getBOAPI().query("BO_EU_CPQ_COMPONENT_PART_PROPERTIES_GRID").bindId(bindId).list();
        		if(bos.size()>0) {
        			for (BO bo : bos) {
					String bm=	bo.getString("BM");//编码
					String mc=	bo.getString("MC");//名称
					String lx=	bo.getString("LX");//类型
					String xh=	bo.getString("XH");//型号
					String dw=	bo.getString("DW");//单位
					String zl=	bo.getString("ZL");//重量
					BO zlBo=new BO();
					zlBo.set("BM", bm);
					zlBo.set("MC", mc);
					zlBo.set("LX", lx);
					zlBo.set("XH", xh);
					zlBo.set("DW", dw);
					zlBo.set("ZL", zl);
					lists.add(zlBo);
					}
        		}
        	}else if (type.equals("装配件")) {
        		
        		//将所用到的原材料的重量，存放到材料用量信息子表中。
            	String sql="select b.TYPE,b.NAME,b.BM as BH,a.MC,a.BM,a.LX,a.XH,a.DW,a.ZL "
            			+ "from  BO_EU_CPQ_COMPONENT_PART_CLYL a ,  BO_EU_CPQ_COMPONENT_PART  b " + 
            			"where  a.BINDID=b.BINDID and  b.STATUS='启用' and  b.TYPE !='采购件'";
            	List<RowMap> maps=DBSql.getMaps(sql);
            	Map<String, List<RowMap>> hMap=new HashMap<String, List<RowMap>>();
            	for (RowMap rowMap : maps) {
					String bh=rowMap.getString("BH");//物料编码
					if(hMap.containsKey(bh)) {
					List<RowMap>	zlbos=hMap.get(bh);
					zlbos.add(rowMap);
					hMap.put(bh, zlbos);
					}else {
						List<RowMap>	zlbos=new ArrayList<>();
						zlbos.add(rowMap);
						hMap.put(bh, zlbos);
						
					}
				}
            	//获取装配件的子项数据
            	List<BO> bos=SDK.getBOAPI().query("BO_EU_CPQ_COMPONENT_PART_GRID").bindId(bindId).list();
            	if(bos.size()>0) {
            		Map<String, BO> map=new HashMap<String, BO>();
            		for (BO bo : bos) {
						String bm=bo.getString("BM");//物料编码
						String sl=bo.getString("SL");//数量
					    //获取当前物料的材料用量信息
						if(hMap.containsKey(bm)) {
							List<RowMap>	zlbos=hMap.get(bm);
							for (RowMap rowMap : zlbos) {
								String clbm=rowMap.getString("BM");//获取材料编码
								String clzl=rowMap.getString("ZL");//获取材料重量
								//计算材料的重量，数量乘以重量
								BigDecimal num1=new BigDecimal( sl);
			        			BigDecimal num2=new BigDecimal(clzl);			        			
								if(map.containsKey(clbm)) {									
									BO clBo=map.get(clbm);//获取已计算的材料信息
									String old=clBo.getString("ZL");//获取已计算的材料重量
									BigDecimal num3=new BigDecimal(old);
									//将已经计算出来的材料重量+当前的材料重量
									clBo.set("ZL", num3.add(num2.multiply(num1)));									
								}else {
									BO clBo=new BO();
									clBo.set("BM", clbm);
									clBo.set("MC", rowMap.getString("MC"));
									clBo.set("LX", rowMap.getString("LX"));
									clBo.set("XH", rowMap.getString("XH"));
									clBo.set("DW", rowMap.getString("DW"));
									clBo.set("ZL", num2.multiply(num1));
									map.put(clbm, clBo);
								}
							}
						}
						
								
					}
            		for (String key : map.keySet()) {
            			lists.add(map.get(key));
					}
            		
            	}
            	
			}
           //将数据回写到材料用量信息子表中
        	List<BO> bos=SDK.getBOAPI().query("BO_EU_CPQ_COMPONENT_PART_CLYL").bindId(bindId).list();
        	if(bos.size()>0) {
        		SDK.getBOAPI().removeByBindId("BO_EU_CPQ_COMPONENT_PART_CLYL", bindId);
        	}
        	
        	SDK.getBOAPI().create("BO_EU_CPQ_COMPONENT_PART_CLYL", lists, bindId, pec.getUserContext().getUID());
        	
        }
	}

}
